package com.sailing.xjpb.ipaas.client.core.repository;

import com.sailing.xjpb.common.EJBGenerator;
import com.sailing.xjpb.ipaas.client.core.IpaasHttpClient;
import com.sailing.xjpb.ipaas.client.entity.bo.InSaveSipUserBO;
import com.sailing.xjpb.ipaas.client.entity.dto.SipUserInfoDTO;
import com.sailing.xjpb.ipaas.client.entity.dto.SipUserOrgDTO;
import com.sailing.xjpb.ipaas.client.entity.dto.UserLogInDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpMethod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户相关接口
 *
 * @author 种传民 2019-07-08
 * @author YaoWei 2020-12-29
 */
public class SipUserInfoRepository {

    @Autowired
    private IpaasHttpClient ipaasHttpClient;

    /**
     * 根据orgId查询该组织下的所有用户信息
     */
    public List<SipUserInfoDTO> findChildUser(String orgId) {
        Map<String, Object> paramMap = getOrgIdParamsMap(orgId);
        String findChildUserUrl = "/external-interface/orgUser/findChildUser/{orgId}";
        return this.getUserList(findChildUserUrl, paramMap);
    }

    /**
     * 根据orgCode查询该组织下的所有用户信息
     *
     * @param orgId 组织机构编码
     */
    public List<SipUserInfoDTO> findChildAllUser(String orgId) {
        String url = "/external-interface/orgUser/findChildAllUserByParamsNoPage/{orgId}";
        Map<String, Object> paramMap = getOrgIdParamsMap(orgId);
        return ipaasHttpClient.list(url, HttpMethod.POST, SipUserInfoDTO.class, paramMap, new HashMap<>(1));
    }

    /**
     * 根据userId获得用户信息
     *
     * @param userId 用户ID
     */
    public SipUserInfoDTO findUserByUserId(String userId) {
        Map<String, Object> paramMap = new HashMap<>(1);
        paramMap.put("userId", userId);
        String findUserByUserIdUrl = "/external-interface/rmAuca/findUserByUserId/{userId}";
        return this.getUser(findUserByUserIdUrl, paramMap);
    }

    /**
     * 修改用户信息
     *
     * @param sipUserInfoDTO 用户信息
     */
    public Boolean update(SipUserInfoDTO sipUserInfoDTO) {
        String findUserByUserIdUrl = "/external-interface/user/updateUser";
        InSaveSipUserBO inSaveSipUserBO = EJBGenerator.convert(sipUserInfoDTO, InSaveSipUserBO.class);
        return ipaasHttpClient.exchange(findUserByUserIdUrl, HttpMethod.PUT, Boolean.class, inSaveSipUserBO);
    }

    /**
     * 查找组织下具有角色的所有人员信息
     *
     * @param authorization 认证信息
     * @param orgCode       组织编码
     * @param orgMode       组织模式
     * @param roleCode      角色名称
     * @author YaoWei createDate 2020年10月30日 下午5:16:57
     */
    public List<SipUserOrgDTO> listRoleUsers(String authorization, String orgCode, String orgMode, String roleCode) {
        String uri = "/external-interface/user/queryUserInfoByOrgCodeRoleCode?orgCode=" + orgCode + "&orgMode="
                + orgMode + "&roleCode=" + roleCode;
        return ipaasHttpClient.list(uri, HttpMethod.GET, authorization, SipUserOrgDTO.class);
    }

    private SipUserInfoDTO getUser(String url, Map<String, Object> params) {
        return ipaasHttpClient.exchange(url, HttpMethod.GET, SipUserInfoDTO.class, params);
    }

    private List<SipUserInfoDTO> getUserList(String url) {
        return this.getUserList(url, new HashMap<>());
    }

    private List<SipUserInfoDTO> getUserList(String url, Map<String, Object> params) {
        return ipaasHttpClient.list(url, HttpMethod.GET, SipUserInfoDTO.class, params);
    }

    /**
     * 获取当前在线数(除了当前登录账号 和admin 账号)
     *
     * @param orgId: 组织id
     * @return 当前在线数
     */
    public UserLogInDTO getOnlineUsers(Pageable pageable, String orgId) {
        String url = "/external-interface/findOnlineUserPage?page={page}&size={size}&orgId={orgId}";
        Map<String, Object> uriVariables = new HashMap<>(3);
        uriVariables.put("page", pageable.getPageNumber() + 1);
        uriVariables.put("size", pageable.getPageSize());
        uriVariables.put("orgId", orgId);
        return ipaasHttpClient.exchange(url, HttpMethod.GET, UserLogInDTO.class, uriVariables);
    }

    /**
     * 获取当前在线数(除了当前登录账号 和admin 账号)
     *
     * @param orgId: 组织id
     *               createDate: 2021/12/14 17:50
     *               editDescription:
     *               editUser: 付玉龙
     *               editDate: 2021/12/14 17:50
     */
    public Page<UserLogInDTO.OnlineUserDTO> pageOnlineUsers(Pageable pageable, String orgId) {
        UserLogInDTO userLogInDTO = getOnlineUsers(pageable, orgId);
        return new PageImpl<>(userLogInDTO.getData(), pageable, userLogInDTO.getTotal());
    }

    /**
     * 根据orgId查询该组织下的所有用户信息
     */
    public List<SipUserInfoDTO> findUserByOrgId(String orgId) {
        Map<String, Object> paramMap = getOrgIdParamsMap(orgId);
        String findChildUser = "/external-interface/rmAuca/findChildUser/{orgId}";
        return this.getUserList(findChildUser, paramMap);
    }

    /**
     * 查询有组织机构的所有用户id
     */
    public List<SipUserInfoDTO> findAllUsers() {
        String queryOrgUserInfo = "/external-interface/rmAuca/queryOrgUserInfo";
        return this.getUserList(queryOrgUserInfo);
    }

    private Map<String, Object> getOrgIdParamsMap(String orgId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("orgId", orgId);
        return paramMap;
    }
}
